<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChUserGroup;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;

class ChUserGroupMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChUserGroup::class;
    }

    public function findById(mixed $id): ChUserGroup|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('id',$id)
            ->first();
    }


    /**
     * 分页获取分组列表
     * @param array $params
     * @return Collection|array
     */
    public function getGroupList(array $params,array $fields = ['*']): \Hyperf\Database\Model\Collection|array
    {
        $list = $this->handleSearch($this->model::query()->select($fields),$params)->orderBy('sort')->get();
        return $list;
    }

    public function handleSearch(Builder $query, array $params): Builder
    {
        $query->where('main_uid',$params['main_uid']);

        if(!empty($params['keyword'])){
            $query->where('name','like','%'.$params['keyword'].'%');
        }

        return $query;
    }
}